package com.chuangyang.fixboxmaster.ui.fragment.orderprice;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.Response;

import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.CostItem;
import com.chuangyang.fixboxmaster.bean.OrderPriceItem;
import com.chuangyang.fixboxmaster.bean.OrderPriceItemInfo;
import com.chuangyang.fixboxmaster.bean.PriceItem;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.ModuleActivity;
import com.chuangyang.fixboxmaster.ui.adapter.PriceDetailAdapter;
import com.chuangyang.fixboxmaster.ui.fragment.BaseFragment;
import com.chuangyang.fixboxmaster.ui.widgets.PinnedSectionListView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * Created by Administrator on 2015/6/3.
 * 2.0 版添加
 */
public class CostDetailFragment02 extends BaseFragment implements View.OnClickListener{


    private View mContentView;

    private PinnedSectionListView mCostDetailLV;
    private TextView mTotalCostView;
    private ArrayList<OrderPriceItem> mOrderPriceItems = new ArrayList<>();
    private ArrayList<PriceItem> mPriceItem = new ArrayList<>();
    private PriceDetailAdapter mDetailAdapter;

    private String mOrderID;
    public static CostDetailFragment02 newInstance() {
        return new CostDetailFragment02();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_cost_detail, container, false);
        Bundle bundle = getArguments();
        if (bundle != null)
        {
            mOrderID = bundle.getString(ModuleActivity.ORDER_ID);
        }
        initView();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
        loadData();
        initData();
    }

    private void initView(){
        mCostDetailLV = (PinnedSectionListView)mContentView.findViewById(R.id.cost_detail_listview);
        mTotalCostView = (TextView)mContentView.findViewById(R.id.total_cost);
        mTotalCostView.setOnClickListener(this);
        TextView mBtnOk=(TextView)mContentView.findViewById(R.id.btn_ok);
        mBtnOk.setVisibility(View.GONE);

    }

    protected void loadData(){
        HashMap<String, String> param = new HashMap<>();
        param.put("orderId",mOrderID);
        addRequest(new GsonRequest<>(FixBoxApi.GET_COSTITEM, param, OrderPriceItemInfo.class, responseListener));
    }

    private Response.Listener<OrderPriceItemInfo> responseListener =new Response.Listener<OrderPriceItemInfo>(){

        @Override
        public void onResponse(OrderPriceItemInfo response) {
            if (response.success == 1)
            {
                mOrderPriceItems.clear();
                mOrderPriceItems.addAll(Arrays.asList(response.result));
                generatePriceItems();
                initData();
            }
        }
    };

    private void initData(){
        if (mOrderPriceItems == null)
            return;
        // 初始化费用列表
        mDetailAdapter = new PriceDetailAdapter(mPriceItem);
        mCostDetailLV.setAdapter(mDetailAdapter);
        setTotalPrice();
        showResult();
    }

    private void generatePriceItems(){

        for (OrderPriceItem item:mOrderPriceItems)
        {
            PriceItem sectionItem = new PriceItem();

            if (item.costitem != null)
            {
                // section
                sectionItem.type = PriceItem.SECTION;
                sectionItem.sectionTitle = item.title;
                mPriceItem.add(sectionItem);

                // item
                for (CostItem costItem:item.costitem)
                {
                    PriceItem priceItem = new PriceItem();
                    priceItem.type = PriceItem.ITEM;
                    priceItem.costItem = costItem;
                    mPriceItem.add(priceItem);
                }
            }
        }
    }

    private String getCostItemJson(){

        Gson gson = new Gson();
        String json = gson.toJson(mOrderPriceItems);

        return json;
    }



    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.total_cost:
                getActivity().finish();
                break;
            default:
                break;
        }
    }
    private void setTotalPrice(){

        int totalPrice = 0;

        for (OrderPriceItem item : mOrderPriceItems)
        {
            if (item.costitem != null)
            {
                for (CostItem costItem:item.costitem)
                {
                    totalPrice += costItem.num*costItem.price;
                }
            }
        }
        mTotalCostView.setText("合计: ￥" +totalPrice);
    }

   /* @Override
    public ListView getListView() {
        return mCostDetailLV;
    }*/ // 2.0版注释
}
